package cn.wzy.dao;

import cn.wzy.common.PageInfo;
import cn.wzy.pojo.User;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author nihaowzy
 * @date 2020/12/22 17:04
 */
public class UserDao extends BaseDao<User> {

    /**
     * 根据姓名和密码查询数据
     * @param username
     * @param password
     * @return
     * @throws Exception
     */
    public User SelectUserByUsernameAndPassWord(String username ,String password) throws Exception {
        String sql = "select id, username, password, realname, role, deleted, img, create_time createTime, modify_time  modifyTime , deleted_time deletedTime from user  where username = ? and password = ? ";
        User user = super.selectOne(sql, User.class, username, password);
        return user;
    }

    /**
     * 模糊查询得到的数据信息
     * @param params 模糊查询的参数
     * @param page 页码
     * @param limit 每页数据条数
     * @return
     */
    public PageInfo<User> selectPage(Map<String ,String> params,Integer page ,Integer limit) throws Exception {
        String  sql = "select id, username, password, realname, role, deleted, img, create_time createTime, modify_time  modifyTime , deleted_time deletedTime from user  where 1=1 ";
        //用户名
        if (params.containsKey("username")){
            sql = sql + "and username like '%"+params.get("username")+"%' ";
        }
        //真实姓名
        if (params.containsKey("realname")){
            sql = sql + "realname like ' %"+params.get("realname")+"% ' ";
        }
        //用户角色 管理员 或者是 员工
        if (params.containsKey("role")){
            sql = sql + "and role = "+params.get("role")+" ";
        }
        //用户是否离职
        if (params.containsKey("deleted")){
            sql = sql + "and deleted = "+params.get("deleted")+" ";
        }
        return super.selectPage(sql,User.class,page,limit);
    }

    /**
     * 将员工密码修改为初始密码
     * @param id
     * @param oriPassword
     */
    public void resetPwd(String id, String oriPassword) throws Exception {
        String sql = "update user set password = ? where id = ?";
        super.update(sql,oriPassword,id);
    }

    /**
     * 将员工的状态改为离职状态 让后添加离职时间
     * @param id
     * @param deleted
     */
    public void del(String id ,Integer deleted) throws Exception {
        String sql = "update user set deleted = ? ,deleted_time = ? where id = ? ";
        super.update(sql,deleted,new Date(),id);
    }

    public User selectUserByRealname(String realname) throws Exception {
        String sql = "select id from user where realname = ?";
        return super.selectOne(sql,User.class,realname);
    }
    /**
     * 添加用户
     * @param realname
     * @param username
     * @param password
     * @param role
     * @param deleted
     * @throws Exception
     */
    public void add(  String realname ,String username ,String password ,Integer role,Integer deleted ) throws Exception {
        String sql = "insert into user ( username ,password ,realname, role , deleted ) values (?,?,?,?,?)";

        super.update(sql,username,password,realname,role,deleted);
    }


    /**
     * 客户对应的所有业务员
     * @param userDeleteNot
     * @param userRoleSalesman
     * @return
     */
    public List<User> selectByDeletedAndRole(Integer userDeleteNot, Integer userRoleSalesman) throws Exception {
        String sql = "select id ,realname  from user where role = ? and deleted = ?";

        return super.selectAll(sql,User.class,userRoleSalesman , userDeleteNot);
    }

    /**
     * 修改用户密码
     * @param id
     * @param newPassword
     */
    public void updatePassword(Integer id, String newPassword) throws Exception {
        String sql = "update user set password = ? where id = ?";
        super.update(sql,newPassword,id);
    }

    /**
     * 上传图片
     * @param id
     * @param imgUrl
     */
    public void uploadImg(Integer id, String imgUrl) throws Exception {
        System.out.println(id);
        System.out.println("图片路径："+imgUrl);
        String sql =  "update user set img = ? where id = ?";
        super.update(sql,imgUrl,id);
    }
}
